Getting Started¶
This notebook will show you examples on how to use the module.
Using the LeafMap module¶
Loading the module¶
In [1]:
Copied!
from cookiecutter_practice import LeafMap
from cookiecutter_practice import LeafMap
Creating a LeafMap Map instance¶
In [2]:
Copied!
m = LeafMap.Map(center=(40, -100), zoom=4, height='300px')
m = LeafMap.Map(center=(40, -100), zoom=4, height='300px')
In [3]:
Copied!
m
m
Out[3]:
Adding a new basemap¶
In [4]:
Copied!
m.add_basemap(basemap='OpenTopoMap')
m.add_basemap(basemap='OpenTopoMap')
Adding layer control¶
Adds a widget for controlling the map layers.
In [5]:
Copied!
m.add_layer_control()
m.add_layer_control()
Removing layer control¶
In [6]:
Copied!
# Uncomment the following line to remove the layer control
#m.remove_layer_control()
# Uncomment the following line to remove the layer control
#m.remove_layer_control()
Adding a vector layer¶
You may pass a URL to a GeoJSON data.
In [7]:
Copied!
url = "https://github.com/opengeos/datasets/releases/download/places/las_vegas_buildings.geojson"
m.add_vector(name="Las Vegas Buildings", url=url)
url = "https://github.com/opengeos/datasets/releases/download/places/las_vegas_buildings.geojson"
m.add_vector(name="Las Vegas Buildings", url=url)
In [8]:
Copied!
m.center = (36.1, -115.2)
m.zoom = 12
m.center = (36.1, -115.2)
m.zoom = 12
In [9]:
Copied!
m
m
Out[9]:
You may also pass GeoDataFrame
In [10]:
Copied!
import pandas as pd, numpy as np
import geopandas as gpd
numpoints = 10
center = (46.91, 7.43)
df = pd.DataFrame(
{'Conc': 1 * np.random.randn(numpoints) + 17,
'Longitude': 0.0004 * np.random.randn(numpoints) + center[1],
'Latitude': 0.0004 * np.random.randn(numpoints) + center[0]})
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude))
import pandas as pd, numpy as np
import geopandas as gpd
numpoints = 10
center = (46.91, 7.43)
df = pd.DataFrame(
{'Conc': 1 * np.random.randn(numpoints) + 17,
'Longitude': 0.0004 * np.random.randn(numpoints) + center[1],
'Latitude': 0.0004 * np.random.randn(numpoints) + center[0]})
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude))
In [11]:
Copied!
m.add_vector(
name="Release",
geo_data=gdf,
style={'color': 'black', 'radius':8, 'fillColor': '#3366cc', 'opacity':0.5, 'weight':1.9, 'dashArray':'2', 'fillOpacity':0.6},
hover_style={'fillColor': 'red' , 'fillOpacity': 0.2},
point_style={'radius': 5, 'color': 'red', 'fillOpacity': 0.8, 'fillColor': 'blue', 'weight': 3, 'type':'circle'}
)
m.add_vector(
name="Release",
geo_data=gdf,
style={'color': 'black', 'radius':8, 'fillColor': '#3366cc', 'opacity':0.5, 'weight':1.9, 'dashArray':'2', 'fillOpacity':0.6},
hover_style={'fillColor': 'red' , 'fillOpacity': 0.2},
point_style={'radius': 5, 'color': 'red', 'fillOpacity': 0.8, 'fillColor': 'blue', 'weight': 3, 'type':'circle'}
)
In [12]:
Copied!
m.center = center
m.zoom = 12
m
m.center = center
m.zoom = 12
m
Out[12]:
Using the FoliumMap module¶
Loading the module¶
In [13]:
Copied!
from cookiecutter_practice import FoliumMap
from cookiecutter_practice import FoliumMap
Creating a FoliumMap Map instance¶
In [14]:
Copied!
f = FoliumMap.Map(cebter=[40.75, -73.95], zoom=12, height='100%')
f = FoliumMap.Map(cebter=[40.75, -73.95], zoom=12, height='100%')
Adding a new basemap¶
In [15]:
Copied!
f.add_basemap('CartoDB positron')
f.add_basemap('CartoDB positron')
'CartoDB positron'
Adding a vector layer¶
You may pass a URL to a GeoJSON data.
In [16]:
Copied!
f.add_vector(name="Map1", url='https://raw.githubusercontent.com/python-visualization/folium-example-data/main/new_york_boroughs.zip', style={'color': 'black', 'fillColor': 'blue', 'opacity':0.05, 'weight':1.9, 'dashArray':'2', 'fillOpacity':0.6})
f.add_vector(name="Map1", url='https://raw.githubusercontent.com/python-visualization/folium-example-data/main/new_york_boroughs.zip', style={'color': 'black', 'fillColor': 'blue', 'opacity':0.05, 'weight':1.9, 'dashArray':'2', 'fillOpacity':0.6})
You may also pass GeoDataFrame
In [17]:
Copied!
import geopandas
gdf = geopandas.read_file(
"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/subway_stations.geojson"
)
gdf['href'] = '<a href="' + gdf.url + '">' + gdf.url + "</a>"
gdf['service_level'] = gdf.notes.str.split(', ').apply(lambda x: len([v for v in x if "all" in v]))
gdf['lines_served'] = gdf.line.str.split('-').apply(lambda x: len(x))
colors = ["orange", "yellow", "green", "blue"]
import geopandas
gdf = geopandas.read_file(
"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/subway_stations.geojson"
)
gdf['href'] = '' + gdf.url + ""
gdf['service_level'] = gdf.notes.str.split(', ').apply(lambda x: len([v for v in x if "all" in v]))
gdf['lines_served'] = gdf.line.str.split('-').apply(lambda x: len(x))
colors = ["orange", "yellow", "green", "blue"]
In [18]:
Copied!
import folium
f.add_vector(name="Map2", geo_data=gdf,
marker=folium.Circle(radius=4, fill_color="orange", fill_opacity=0.4, color="black", weight=1),
tooltip=folium.GeoJsonTooltip(fields=["name", "line", "notes"]),
style=lambda x: {
"fillColor": colors[x['properties']['service_level']],
"radius": (x['properties']['lines_served'])*30,
},
highlight_style=lambda x: {"fillOpacity": 0.8},
zoom_on_click=True
)
import folium
f.add_vector(name="Map2", geo_data=gdf,
marker=folium.Circle(radius=4, fill_color="orange", fill_opacity=0.4, color="black", weight=1),
tooltip=folium.GeoJsonTooltip(fields=["name", "line", "notes"]),
style=lambda x: {
"fillColor": colors[x['properties']['service_level']],
"radius": (x['properties']['lines_served'])*30,
},
highlight_style=lambda x: {"fillOpacity": 0.8},
zoom_on_click=True
)
Adding layer control¶
Adds a widget for controlling the map layers.
Note: Add this only after adding all the other layers, unexpected behavior may occur.
In [19]:
Copied!
f.add_layer_control('topright')
f.add_layer_control('topright')
In [20]:
Copied!
f
f
Out[20]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
Copied!